package com.handy.plugin.auth.action;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.handy.plugin.auth.bean.HAuthUser;
import com.handy.webwork.action.ActionForm;

/**
 * 接收用户名口令，返回验证结果。
 * @author rocken.zeng@gmail.com
 *
 */
public class UserLoginStatusAction extends Action
{

	public String execute(){
		if (log.isDebugEnabled()){
			log.info("execute()");
		}

		setFlag(0);//先设置为未登录状态
		// 用户不可用，重新登陆
		if (getSession().getAttribute("user") != null) {
			setFlag(1);
		}
		//log.info("flag:"+getFlag());
		String ret = "";
		switch (getViewFormat()){
		case 1:
			ret = "html";
			break;
		case 2:
			ret = "xml";
			break;
		case 3:
			ret = "json";
			break;
		}
        return ret;
    }

	@SuppressWarnings("unchecked")
	public HashMap sendData() {
		HashMap root = new HashMap();
		root.put("flag", getFlag());
		//root.put("viewFormat", getViewFormat());
		return root;
	}
	
	public void getForm(ActionForm form) {

		if (null != form.getObject("view") && !"".equals(form.getObject("view"))){
			setViewFormat(form.getInteger("view"));
		}
	}
	
    private Integer flag;
    
    /**
     * 返回视图的格式。<br>
     * 1.html输出 <br>
	 * 2.xml输出 <br>
	 * 3.json输出(默认输出) <br> 
     */
    private Integer viewFormat = 3; 
    
    private static final Log log = LogFactory.getLog(UserLoginStatusAction.class);
    
    private HAuthUser user;

	public HAuthUser getUser() {
		return user;
	}
	public void setUser(HAuthUser user) {
		this.user = user;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	public Integer getViewFormat() {
		return viewFormat;
	}

	public void setViewFormat(Integer viewFormat) {
		this.viewFormat = viewFormat;
	}

}
